from django.db import models


class Shops(models.Model):
    """分店模型类"""

    name = models.CharField(max_length=7, unique=True, verbose_name='分店名')
    chicken = models.SmallIntegerField(default=0, verbose_name='鸡剩余量')
    duck = models.SmallIntegerField(default=0, verbose_name='鸭剩余量')
    default_chicken = models.SmallIntegerField(default=0, null=True, verbose_name='默认鸡单价')
    default_duck = models.SmallIntegerField(default=0, null=True, verbose_name='默认鸭单价')

    class Meta:
        db_table = 'shopmanage_shops'
        verbose_name = '分店'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Clerks(models.Model):
    """店员模型类"""

    IDENTITY_CHOICES = (
        (0, '仅注册'),
        (1, '店员'),
        (2, '老板'),
        (3, '库管')
    )

    shop = models.ForeignKey('Shops', null=True, blank=True, verbose_name='所属分店', on_delete=models.CASCADE)
    # 微信名的长度限制为 6 ~ 20
    WXname = models.CharField(max_length=20, null=True, blank=True, verbose_name='微信名')
    # 微信号的openid长度限制为 6 ~ 20
    openid = models.CharField(max_length=64, primary_key=True, verbose_name='OpenId')
    # 身份默认为“仅注册”
    identity = models.SmallIntegerField(default=0, choices=IDENTITY_CHOICES, verbose_name='身份')

    class Meta:
        db_table = 'shopmanage_clerks'
        verbose_name = '店员'
        verbose_name_plural = verbose_name
        unique_together = ()

    def __str__(self):
        return self.WXname


class Cost(models.Model):
    """成本支出模型类"""

    shop = models.ForeignKey('Shops', verbose_name='所属分店', on_delete=models.CASCADE)
    date = models.DateField(verbose_name='记录日期')
    # cost_type = models.SmallIntegerField(default=0, choices=COST_TYPE_CHOICES, verbose_name='成本类型')
    cost_type = models.CharField(max_length=7, verbose_name='成本类型')
    money = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='支出金额')

    class Meta:
        db_table = 'shopmanage_cost'
        verbose_name = '支出'
        verbose_name_plural = verbose_name
        ordering = ['date']

    def __str__(self):
        return self.cost_type


class Distribution(models.Model):
    """分销模型类"""

    shop = models.ForeignKey('Shops', verbose_name='分销分店', on_delete=models.CASCADE)
    date = models.DateField(verbose_name='分销时间')
    chicken = models.SmallIntegerField(default=0, verbose_name='鸡分销量')
    duck = models.SmallIntegerField(default=0, verbose_name='鸭分销量')

    class Meta:
        db_table = 'shopmanage_distribution'
        verbose_name = '分销'
        verbose_name_plural = verbose_name
        ordering = ['date']

    def __str__(self):
        return self.date


class Sale(models.Model):
    """销售模型类"""

    shop = models.ForeignKey('Shops', verbose_name='销售分店', on_delete=models.CASCADE)
    date = models.DateField(verbose_name='销售时间')
    chicken_count = models.SmallIntegerField(default=0, verbose_name='鸡销售量')
    duck_count = models.SmallIntegerField(default=0, verbose_name='鸭销售量')
    chicken_unit_price = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='鸡单价')
    duck_unit_price = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='鸭单价')
    money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='销售金额')

    class Meta:
        db_table = 'shopmanage_sale'
        verbose_name = '销售'
        verbose_name_plural = verbose_name
        ordering = ['date']

    def __str__(self):
        return self.date


class Purchase(models.Model):
    """进库模型类"""

    BUYER_CHOICES = (
        (0, '金龙水产'),
        (1, '严明水产'),
        (2, '新货站水产')
    )

    supplier = models.SmallIntegerField(default=0, choices=BUYER_CHOICES, verbose_name='供货方')
    date = models.DateField(verbose_name='入库时间')
    chicken_count = models.SmallIntegerField(default=0, verbose_name='鸡入库量')
    duck_count = models.PositiveIntegerField(default=0, verbose_name='鸭入库量')
    # chicken_unit_price = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='鸡单价')
    # duck_unit_price = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='鸭单价')
    # money = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name='入库金额')

    class Meta:
        db_table = 'shopmanage_purchase'
        verbose_name = '进货'
        verbose_name_plural = verbose_name
        ordering = ['date']

    def __str__(self):
        return self.supplier


class Transfer(models.Model):
    """调货模型类"""

    STATUS_CHOICES = (
        (0, '未处理'),
        (1, '同意'),
        (2, '拒绝')
    )

    apply_shop = models.ForeignKey('Shops', verbose_name='请求分店', related_name='apply_shop', null=True,
                                   on_delete=models.SET_NULL)
    transfer_shop = models.ForeignKey('Shops', verbose_name='调货分店', related_name='transfer_shop', null=True,
                                      on_delete=models.SET_NULL)
    handler = models.ForeignKey('Clerks', default=None, verbose_name='处理人', related_name='handler', null=True,
                                on_delete=models.SET_NULL)
    apply_date = models.DateField(auto_now_add=True, verbose_name='申请时间')
    handle_date = models.DateField(blank=True, null=True, verbose_name='处理时间')
    chicken = models.SmallIntegerField(default=0, verbose_name='请求鸡数')
    duck = models.SmallIntegerField(default=0, verbose_name='请求鸭数')
    status = models.SmallIntegerField(default=0, choices=STATUS_CHOICES, verbose_name='调货状态')

    class Meta:
        db_table = 'shopmanage_transfer'
        verbose_name = '调货'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.transfer_shop.name + ' -> ' + self.apply_shop.name
